﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPractice.Models;

namespace MvcPractice.Controllers
{
    public class TestController : Controller
    {

        //
        // GET: /Test/

        public ActionResult Index()
        {
            using (var db = new csc440_fall2012_team2Entities())
            {
                return View(db.TEST_MIKE.ToList());
            }
        }

 //       
 //        POST: /Test/Index/All

        [HttpPost]
        public ActionResult Index(String selection)
        {
            using (var db = new csc440_fall2012_team2Entities())
            {
                if (selection.Equals("Test"))
                {
                    var userList =
                        from t in db.TEST_MIKE
                        where t.Test_Name.Contains(selection)
                        select t;
                    foreach (var test in userList)
                    {
                        Console.WriteLine(test.Test_Name);
                    }

                    return View(userList);
                }
                else
                {
                    return View(db.TEST_MIKE.ToList());
                }
            }
        }

        //
        // GET: /Test/PageList/All

        public ActionResult PageList()
        {
            using (var db = new csc440_fall2012_team2Entities())
            {
                return View(db.TEST_MIKE.ToList());
            }
        }

        //
        // POST: /Test/PageList/All

        [HttpPost]
        public ActionResult PageList(String selection)
        {
            using (var db = new csc440_fall2012_team2Entities())
            {
                if(selection.Equals("Test"))
                {
                    var userList =
                        from t in db.TEST_MIKE
                        where t.Test_Name.Contains(selection)
                        select t;
                    foreach (var test in userList)
                    {
                        Console.WriteLine(test.Test_Name);
                    } 

                    return View(userList);
                }else
                {
                    return View(db.TEST_MIKE.ToList());
                }
            }
        }

        //
        // GET: /Test/Details/5

        public ActionResult Details(int id)
        {
            using (var db = new csc440_fall2012_team2Entities())
            {
                TEST_MIKE test = db.TEST_MIKE.First(t => t.Test_ID == id);
                return View(test);
            }
        }

        //
        // GET: /Test/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Test/Create

        [HttpPost]
        public ActionResult Create(TEST_MIKE test)
        {
            try
            {
                using (var db = new csc440_fall2012_team2Entities())
                {
                    db.TEST_MIKE.AddObject(test);
                    db.SaveChanges();
                }
            }
            catch
            {
                return View();
            }
            return View();
        }

        //
        // GET: /Test/Edit/5

        public ActionResult Edit(int id)
        {
            using (var db = new csc440_fall2012_team2Entities())
            {
            TEST_MIKE test = db.TEST_MIKE.First(t => t.Test_ID == id);
            return View(test);
            }
        }

        //
        // POST: /Test/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, TEST_MIKE test)
        {
            try
            {
                using (var db = new csc440_fall2012_team2Entities())
                {
                    TEST_MIKE getTest = db.TEST_MIKE.First(t => t.Test_ID == id);
                    getTest.Test_Desc = test.Test_Desc;
                    getTest.Test_ID = test.Test_ID;
                    getTest.Test_Name = test.Test_Name;
                    db.SaveChanges();
                }
            }
            catch
            {
                return View();
            }
            return View();
        }

        //
        // GET: /Test/Delete/5

        public ActionResult Delete(int id)
        {
            using (var db = new csc440_fall2012_team2Entities())
            {
                TEST_MIKE test = db.TEST_MIKE.First(t => t.Test_ID == id);
                return View(test);
            } 
        }

        //
        // POST: /Test/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, TEST_MIKE test)
        {           
            try
            {
                using (var db = new csc440_fall2012_team2Entities())
                {
                    test = db.TEST_MIKE.First(t => t.Test_ID == id);
                    db.DeleteObject(test);
                    db.SaveChanges();
                }
            }
            catch
            {
                using (var db = new csc440_fall2012_team2Entities())
                {
                    return View("Index", db.TEST_MIKE.ToList());
                }
            }
            using (var db = new csc440_fall2012_team2Entities())
            {
                return View("Index", db.TEST_MIKE.ToList());
            }
        }
    }
}
